package com.hljcj.boot.service;

import com.hljcj.boot.bean.SysStaff;
import com.hljcj.boot.dao.StaffDao;
import com.hljcj.boot.dao.StaffRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@Service
@Transactional
public class StaffService {

	@Resource
	private StaffRepository staffRepository;

	@Resource
	private StaffDao staffDao;

	public Page<SysStaff> queryStaff(int pageNo, int pageSize) {
		Pageable pageable = PageRequest.of(pageNo - 1, pageSize);
		Page<SysStaff> staffls = staffRepository.findAll(pageable);
		return staffls;
	}

	public Page<SysStaff> queryStaff(SysStaff sysStaff, int pageNo, int pageSize) throws Exception {
		Page<SysStaff> page = staffDao.queryStaff(sysStaff, pageNo, pageSize);
		return page;
	}

	@Transactional
	public String addStaff(SysStaff staff) {
		List<SysStaff> tmp = staffRepository.findByStaffName(staff.getStaffName());
		if (null == tmp || tmp.size() == 0) {
			staffRepository.save(staff);
			return "succ";
		} else {
			return "部门名称：" + staff.getStaffName() + "，重复，保存失败！";
		}
	}

	@Transactional
	public String updStaff(SysStaff staff) {
		List<SysStaff> ls = staffRepository.findByStaffName(staff.getStaffName());
		if (null != ls && ls.size() > 0) {
			SysStaff temp = ls.get(0);
			if (temp.getStaffId() == staff.getStaffId()) {
				staffRepository.save(staff);
				return "succ";
			} else {
				return "部门名称：" + staff.getStaffName() + "，重复，保存失败！";
			}
		} else {
			staffRepository.save(staff);
			return "succ";
		}
	}

	/**
	 * 删除一个部门
	 */
	@Transactional
	public void delStaff(Integer staffId) {
		staffRepository.deleteById(staffId);
	}

}
